/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: JdoAssignedInstrumentDAO.java
 * Date: 01/03/2011
 */

package com.ingenius.sau.server.persistence.jdo;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

import com.ingenius.sau.server.persistence.AssignedInstrumentDAO;
import com.ingenius.sau.shared.model.core.Indicator2Evaluate;
import com.ingenius.sau.shared.model.instruments.AssignedInstrument;
import com.ingenius.sau.shared.model.instruments.Instrument;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>JdoAssignedInstrumentDAO</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class JdoAssignedInstrumentDAO implements AssignedInstrumentDAO {

	private static Logger logger = Logger
			.getLogger(JdoAssignedInstrumentDAO.class.getName());

	private JdoDocumentDAO documentDAO;
	private JdoQuestionDAO questionDAO;

	public JdoAssignedInstrumentDAO() {
		documentDAO = new JdoDocumentDAO();
		questionDAO = new JdoQuestionDAO();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#save(java.lang.Object)
	 */
	@Override
	public Boolean save(AssignedInstrument object) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Boolean result = false;
		try {
			logger.info("Saving object...");
			tx.begin();
			String id = object.getIndicator2Evaluate().getId();
			object.setIndicator2Evaluate(null);
			Indicator2Evaluate indicator2Evaluate = pm.getObjectById(
					Indicator2Evaluate.class, id);
			indicator2Evaluate.addAssignedInstrument(object);
			tx.commit();
			result = true;
			logger.fine("ok -> object saved [" + object + "]");

		} catch (Exception e) {
			String msg = "Failed to save object [" + object + "]. ";
			logger.log(Level.SEVERE, msg, e);
			System.out.println(msg + e.getMessage());

		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#update(java.lang.Object)
	 */
	@Override
	public Boolean update(AssignedInstrument copy) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Transaction tx = pm.currentTransaction();
		Boolean result = false;
		try {
			logger.info("Updating object...");
			tx.begin();
			AssignedInstrument save = pm.getObjectById(
					AssignedInstrument.class, copy.getId());
			save.setScore(copy.getScore());
			save.setInstrumentType(copy.getInstrumentType());
			save.setInstrumentId(copy.getInstrumentId());
			tx.commit();
			result = true;
			logger.fine("ok -> object updated [" + save + "]");

		} catch (Exception e) {
			String msg = "Failed to update object [" + copy + "]. ";
			logger.log(Level.SEVERE, msg, e);
			System.out.println(msg + e.getMessage());

		} finally {
			if (tx.isActive()) {
				tx.rollback();
			}
			pm.close();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#delete(java.lang.String)
	 */
	@Override
	public Boolean delete(String id) {
		AssignedInstrument instrument = get(id);
		if (instrument != null
				&& instrument.getInstrumentType().equals(
						Instrument.TYPE_DOCUMENT)) {
			JdoBlobFileDAO.getInstance().delete(instrument.getInstrumentId());
		}
		return JdoUtil.delete(AssignedInstrument.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#get(java.lang.String)
	 */
	@Override
	public AssignedInstrument get(String id) {
		return JdoUtil.get(AssignedInstrument.class, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#count(java.lang.List)
	 */
	@Override
	public Integer count(List<Filter> filters) {
		return JdoUtil.count(AssignedInstrument.class, filters);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.server.persistence.DAO#getRange(java.util.List,
	 * java.lang.String, int, int)
	 */
	@Override
	public List<AssignedInstrument> getRange(List<Filter> filters,
			String orderBy, int start, int limit) {

		List<AssignedInstrument> result = JdoUtil.getRange(
				AssignedInstrument.class, filters, orderBy, start, limit);

		for (AssignedInstrument instrument : result) {
			try {
				String instrumentDisplay = "";
				if (instrument.getInstrumentType().equals(
						Instrument.TYPE_DOCUMENT)) {
					instrumentDisplay = documentDAO.get(
							instrument.getInstrumentId()).toString();
				} else {
					instrumentDisplay = questionDAO.get(
							instrument.getInstrumentId()).toString();
				}
				instrument.setInstrumentDisplay(instrumentDisplay);
			} catch (Exception e) {
				System.out.println("Error: " + e.getMessage());
			}
		}

		return result;
	}

}
